package singletontest;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 这样实现的一个弊端就是违反了“职责单一原则”，完善后的代码如下
 *
 * @author liuziyang
 * @create 2019-10-14 20:55
 */
public class SingletonTest11 {
    public static SingletonTest11 getInstance() {
        return Singleton.INSTANCE.getInstance();
    }

    private SingletonTest11() {
    }

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonTest11.getInstance().hashCode());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonTest11.getInstance().hashCode());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonTest11.getInstance().hashCode());
            }
        }).start();
    }

    public enum Singleton {
        INSTANCE;
        private SingletonTest11 myObject = null;

        Singleton() {
            myObject = new SingletonTest11();
        }

        public SingletonTest11 getInstance() {
            return myObject;
        }
    }
}
